package com.cleverest.cleverestdiary.service.impl;

import com.cleverest.cleverestdiary.mapper.ScheduleCategoryMapper;
import com.cleverest.cleverestdiary.pojo.ScheduleCategory;
import com.cleverest.cleverestdiary.service.ScheduleCategoryService;
import com.cleverest.cleverestdiary.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class ScheduleCategoryServiceImpl implements ScheduleCategoryService {
    @Autowired
    private ScheduleCategoryMapper scheduleCategoryMapper;


    @Override
    public ScheduleCategory findByScheduleCategoryName(String name) {
        ScheduleCategory sc = scheduleCategoryMapper.findByScheduleCategoryName(name);
        return sc;
    }
    @Override
    public void add(ScheduleCategory scheduleCategory) {
        //补充属性值
        scheduleCategory.setCreateTime(LocalDateTime.now());
        scheduleCategory.setUpdateTime(LocalDateTime.now());

        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        scheduleCategory.setCreateUser(userId);
        scheduleCategoryMapper.add(scheduleCategory);
    }

    @Override
    public List<ScheduleCategory> list() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return scheduleCategoryMapper.list(userId);
    }

    @Override
    public ScheduleCategory findById(Integer id) {
        ScheduleCategory c = scheduleCategoryMapper.findById(id);
        return c;
    }

    @Override
    public void update(ScheduleCategory scheduleCategory) {
        scheduleCategory.setUpdateTime(LocalDateTime.now());
        scheduleCategoryMapper.update(scheduleCategory);
    }

    @Override
    public void delete(Integer id) {
        scheduleCategoryMapper.delete(id);
    }
}